<?php

include_once("Database.class.php");
include_once("User.class.php");
include_once("C2C.class.php");
include_once("B2C.class.php");
include_once("Message.class.php");
include_once("Search.class.php");
include_once("helper.php");


function do_list() {
    $arr = array(
        array("name" => "checklogin", "desc" => ""),
        array("name" => "register", "desc" => ""),
        array("name" => "commitpost", "desc" => ""),
        array("name" => "viewpost", "desc" => ""),
        array("name" => "getpostreply", "desc" => ""),
        array("name" => "replypost", "desc" => ""),
    );
    echo json_encode($arr);
}

function dispatch() {
    $s = get_request("s");
    if ($s == null) {
        return;
    }
    $func = "do_$s";
    if (is_callable($func)) {
        $func();
    } else {
        die("no action.");
    }
}

function res_msg($s,$info = null){
	$result_msg = array(
		array('status'	=>	$s),
		array('info'	=>	$info)
	);
	return json_encode($result_msg);
}

//是否已经注册过
function do_checkreg() {
    $username = get_request("username");
	$user = User::getInstance();
	$res  = $user->checkreg($username);
	echo empty($res)? res_msg('no reg') : res_msg('reged');
}

//注册函数
function do_register() {
    $username = get_request("username");
    $password = get_request("password");
    $nickname = get_request("nickname");
	$data = array(
		"id" => "",
		"username" => "$username",
        "password" => "$password",
        "openid"   => "weixin-opid",
        "nickname" => "$nickname"
    );
	$user = User::getInstance();
	$res  = $user->register($data);
	echo empty($res)? res_msg('fail') : res_msg('success',$res);
}

//新建发帖函数
function do_newpost() {
    $title 		= get_request("title");
    $content 	= get_request("content");
    $user_id 	= get_request("userid");
	$title 		= urldecode($title);
    $content 	= urldecode($content);
	$time 		= date('Y-m-d H:i:s');
	$title_search = scws($title);
	$data = array(
		"id" 			=> "",
		"title"			=> "$title",
        "content" 		=> "$content",
        "user_id"   	=> "$user_id",
        "update_time" 	=> "$time",
        "title_search" 	=> "$title_search"
    );
	$c2c = C2C::getInstance();
	$res = $c2c->newpost($data);
	echo empty($res)? res_msg('fail') : res_msg('success',$res);
}


//回复post内容
function do_postreply(){
	$userid  = get_request("userid");
	$postid  = get_request("postid");
	$content = get_request("content");
	$time 	 = date('Y-m-d H:i:s');
	$content_search = scws($content);
	$data = array(
		"id" 			 => "",
		"posts_id" 		 => "$postid",
		"content" 		 => "$content",
		"user_id" 		 => "$userid",
		"update_time" 	 => "$time",
		"content_search" => "$content_search",
		"replyid" => ""
	);
	$c2c = C2C::getInstance();
	$res = $c2c->postreply($data);	
	//插入消息
	$insertid = $res['id'];	
	$postdetail = $c2c->getpostdetail($postid);
	$ownid 		= $postdetail['user_id'];
	$m_data = array(
		"id" 			=> "",
		"user_id" 		=> "$userid",
		"act" 			=> "replies",
		"actid" 		=> "$insertid",
		"type" 			=> "forum_posts",
		"typeid" 		=> "$postid",
		"ownid" 		=> "$ownid",
		"status" 		=> "1",
		"create_time" 	=> "$time",
		"update_time" 	=> ""
	);
	if($ownid != $userid){	//如果帖子是自身发的，就不插入消息
		$msg 	= Message::getInstance();
		$m_res 	= $msg->create($m_data);
		echo empty($res) || empty($m_res) ? res_msg('fail') : res_msg('success',$res);
	}else{
		echo empty($res) ? res_msg('fail') : res_msg('success',$res);
	}
}


function do_reply2sb(){
	//var_dump($_REQUEST);
 	$userid  = get_request("userid");
	$postsid = get_request("postsid");
	$content = get_request("content");
	$replyid = get_request("replyid");
	$time 	 = date('Y-m-d H:i:s');
	$content_search = scws($content);
	$data = array(
		"id" 			 => "",
		"posts_id" 		 => "$postsid",
		"content" 		 => "$content",
		"user_id" 		 => "$userid",
		"update_time" 	 => "$time",
		"content_search" => "$content_search",
		"replyid" 		 => "$replyid"
	);
	$c2c = C2C::getInstance();
	$res = $c2c->postreply($data);	
	//插入消息
	$insertid = $res['id'];	
	$bereplyeddetail = $c2c->getreplydetail($replyid);
	$ownid 		= $bereplyeddetail['user_id'];
	$m_data = array(
		"id" 			=> "",
		"user_id" 		=> "$userid",
		"act" 			=> "reply2sb",
		"actid" 		=> "$insertid",
		"type" 			=> "replies",
		"typeid" 		=> "$replyid",
		"ownid" 		=> "$ownid",
		"status" 		=> "1",
		"create_time" 	=> "$time",
		"update_time" 	=> ""
	);
	if($ownid != $userid){	//如果帖子是自身发的，就不插入消息
		$msg 	= Message::getInstance();
		$m_res 	= $msg->create($m_data);
		echo empty($res) || empty($m_res) ? res_msg('fail') : res_msg('success',$res);
	}else{
		echo empty($res) ? res_msg('fail') : res_msg('success',$res);
	}
}

//登陆函数
function do_login(){
	$username = get_request("username");
    $password = get_request("password");
	$user = User::getInstance();
	$res  = $user->login($username,$password);
	echo empty($res)? res_msg('fail') : res_msg('success',$res);
}

//读取用户信息
function do_getuserdetail(){
	$id   = get_request("id");
	$user = User::getInstance();
	$res  = $user->getuserdetail($id);
	echo empty($res)? res_msg('fail') : res_msg('success',$res);
}

//读取Post信息详情
function do_getpostdetail(){
	$id = get_request("id");
	$c2c = C2C::getInstance();
	$res = $c2c->getpostdetail($id);
	echo empty($res)? res_msg('fail') : res_msg('success',$res);
}

//读取reply信息详情
function do_getreplydetail(){
	$id = get_request("replyid");
	$c2c = C2C::getInstance();
	$res = $c2c->getreplydetail($id);
	echo empty($res)? res_msg('fail') : res_msg('success',$res);
}

//读取所有postList
function do_getpostlist(){
	$startnum = get_request("startnum");
	$showtime = get_request("showtime");
	$c2c = C2C::getInstance();
	$res = $c2c->getpostlist($startnum,$showtime);
	echo empty($res)? res_msg('fail') : res_msg('success',$res);
}

//读取post中所有replies
function do_getpostallreplies(){
	$postid = get_request('postid');
	$c2c = C2C::getInstance();
	$res = $c2c->getpostallreplies($postid);
	echo empty($res)? res_msg('fail') : res_msg('success',$res);
}

//新增图片
function do_newpic(){
	$imgsrc = $_FILES['imgsrc'];
	$imgid  = get_request("imgid");
	$userid = get_request("userid");
	$c2c = C2C::getInstance();
	$res = $c2c->newpic($imgid,$imgsrc,$userid);
	$id = array("id" => "$imgid");
	echo empty($res)? res_msg('fail') : res_msg('success',$id);
}


//上传头像
function do_uploadhead(){
	$imgsrc = $_FILES['imgsrc'];
	$userid = get_request("userid");
	$user = User::getInstance();
	$res = $user->uploadhead($imgsrc,$userid);
	$id = array("id" => "$userid");
	echo empty($res)? res_msg('fail') : res_msg('success',$id);
}

//初始化post
function do_initpost(){
	$userid = get_request("userid");
	$c2c = C2C::getInstance();
	$res = $c2c->initpost($userid);
	echo empty($res)? res_msg('fail') : res_msg('success',$res);
}



//处理图片路径，将temp中的图片移动到userid根目录下
function do_dealpicpath(){
	$userid = get_request("userid");
	$c2c = C2C::getInstance();
	$res = $c2c->dealpicpath($userid);
	echo empty($res)? res_msg('fail') : res_msg('success',$res);
}

//search查询搜索对应内容
function do_search(){
	$content = get_request("content");
	$content = scws($content);
	$search = Search::getInstance();
	$res = $search->searchIt($content);
	echo empty($res)? res_msg('fail') : res_msg('success',$res);
}

//checkmsg查询当前用户是否有未读信息
function do_checkmsg(){
	$userid = get_request("userid");
	$message = Message::getInstance();
	$res = $message->checkmsg($userid);
	//var_dump($res);
	echo empty($res)? res_msg('fail') : res_msg('success',$res);
}

//已读一个消息
function do_readonemsg(){
	$msgid = get_request("msgid");
	$message = Message::getInstance();
	$res = $message->readonemsg($msgid);
	//var_dump($res);
	echo empty($res)? res_msg('fail') : res_msg('success',$res);
}

//getmsglist查询当前用户所有信息
function do_getmsglist(){
	$userid = get_request("userid");
	$startnum = get_request("startnum");
	$showtime = get_request("showtime");
	$message = Message::getInstance();
	$res = $message->getmsglist($userid,$startnum,$showtime);
	//var_dump($res);
	echo empty($res)? res_msg('fail') : res_msg('success',$res);
}

//已读当前用户的所有信息
function do_readallmsg(){
	$userid = get_request("userid");
	$message = Message::getInstance();
	$res = $message->readallmsg($userid);
	var_dump($res);
	echo empty($res)? res_msg('fail') : res_msg('success',$res);
}

//读取post帖子中 userid1和userid2的对话列表
function do_getdialoglist(){
	$postid  = get_request("postid");
	$userid1 = get_request("userid1");
	$userid2 = get_request("userid2");
	$c2c = C2C::getInstance();
	$res = $c2c->getdialoglist($postid,$userid1,$userid2);
	echo empty($res)? res_msg('fail') : res_msg('success',$res);
}

//分割搜索词汇
function scws($text){
	//导入对应的类库
	require_once('pscws4/pscws4.class.php');
	//新建对象
	$pscws = new PSCWS4('utf8');
	//初始化参数
	$pscws->set_dict('pscws4/etc/dict.utf8.xdb');
	$pscws->set_rule('pscws4/etc/rules.utf8.ini');
	$pscws->set_charset('utf8');
	$pscws->send_text($text);
	$searchWord = '';
	while ($some = $pscws->get_result())
	{	
		foreach($some as $word){
			$searchWord .= $word['word'].' ';
		}
	}
	return $searchWord;
}

dispatch();






